/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.publish.service.impl;

import me.zhengjie.modules.publish.domain.PublishTypeMoney;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.publish.repository.PublishTypeMoneyRepository;
import me.zhengjie.modules.publish.service.PublishTypeMoneyService;
import me.zhengjie.modules.publish.service.dto.PublishTypeMoneyDto;
import me.zhengjie.modules.publish.service.dto.PublishTypeMoneyQueryCriteria;
import me.zhengjie.modules.publish.service.mapstruct.PublishTypeMoneyMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
*
* @description 服务实现
* @author maojinhui
* @date 2020-09-06
**/
@Service
@RequiredArgsConstructor
public class PublishTypeMoneyServiceImpl implements PublishTypeMoneyService {

    private final PublishTypeMoneyRepository publishTypeMoneyRepository;
    private final PublishTypeMoneyMapper publishTypeMoneyMapper;

    @Override
    public List<PublishTypeMoney> queryAllByTypeId(Integer typeId) {
        List<PublishTypeMoney> byTypeId = publishTypeMoneyRepository.findByTypeId(typeId);
        return byTypeId;
    }

    @Override
    public Map<String,Object> queryAll(PublishTypeMoneyQueryCriteria criteria, Pageable pageable){
        Page<PublishTypeMoney> page = publishTypeMoneyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(publishTypeMoneyMapper::toDto));
    }

    @Override
    public List<PublishTypeMoneyDto> queryAll(PublishTypeMoneyQueryCriteria criteria){
        return publishTypeMoneyMapper.toDto(publishTypeMoneyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public PublishTypeMoneyDto findById(Integer id) {
        PublishTypeMoney publishTypeMoney = publishTypeMoneyRepository.findById(id).orElseGet(PublishTypeMoney::new);
        ValidationUtil.isNull(publishTypeMoney.getId(),"请选择正确的发布时间");
        return publishTypeMoneyMapper.toDto(publishTypeMoney);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PublishTypeMoneyDto create(PublishTypeMoney resources) {
        return publishTypeMoneyMapper.toDto(publishTypeMoneyRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PublishTypeMoney resources) {
        PublishTypeMoney publishTypeMoney = publishTypeMoneyRepository.findById(resources.getId()).orElseGet(PublishTypeMoney::new);
        ValidationUtil.isNull( publishTypeMoney.getId(),"PublishTypeMoney","id",resources.getId());
        publishTypeMoney.copy(resources);
        publishTypeMoneyRepository.save(publishTypeMoney);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            publishTypeMoneyRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<PublishTypeMoneyDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PublishTypeMoneyDto publishTypeMoney : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("发布类型", publishTypeMoney.getTypeId());
            map.put("发布类型说明", publishTypeMoney.getTypeName());
            map.put("发布天数", publishTypeMoney.getDayNum());
            map.put("收费金额", publishTypeMoney.getMoney());
            map.put(" createTime",  publishTypeMoney.getCreateTime());
            map.put(" updateTime",  publishTypeMoney.getUpdateTime());
            map.put(" createBy",  publishTypeMoney.getCreateBy());
            map.put(" updateBy",  publishTypeMoney.getUpdatedBy());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}